package com.weixin.paleituo.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.ClassifyContentRecyclerViewAdapter;
import com.weixin.paleituo.adapter.ClassifyMenuRecyclerViewAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.ChildrenBean;
import com.weixin.paleituo.bean.ChildrenBeanX;
import com.weixin.paleituo.bean.ClassifyBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.http.RxHelper;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LoadingDialogUtils;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Author yichao
 * Time  2018/4/1 10:38
 * Dest  分类
 */

public class ClassifyBrandFragment extends BaseFragment {
    @BindView(R.id.yrecycleview_menu_classify_fragment)
    YRecycleview mYrecycleviewMenuClassifyFragment;
    @BindView(R.id.yrecycleview_content_classify_fragment)
    YRecycleview mYrecycleviewContentClassifyFragment;
    private ClassifyMenuRecyclerViewAdapter mClassifyMenuRecyclerViewAdapter;
    private ClassifyContentRecyclerViewAdapter mClassifyContentRecyclerViewAdapter;
    private Dialog mLoadingDialog;
    private String mCategoryId;
    List<ChildrenBeanX> mContentChildrenBeanList=new ArrayList<>();
    private int currentPager=1;


    @Override
    public View initView(Context context) {
        return UIUtils.inflate(mContext, R.layout.classify_fragment);
    }
    public static ClassifyBrandFragment getInstance(ClassifyBean classifyBean) {
        ClassifyBrandFragment classifyBrandFragment = new ClassifyBrandFragment();
        Bundle bundle=new Bundle();
        bundle.putParcelable(Contans.INTENT_DATA, classifyBean);

        classifyBrandFragment.setArguments(bundle);
        return classifyBrandFragment;
    }


    @Override
    protected void initEvent() {
        super.initEvent();
        mYrecycleviewContentClassifyFragment.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {

                Observable observable = ApiUtils.getApi().getClassrecommentgoods(mCategoryId, Contans.defaultToken, 1, Contans.rows,BaseApplication.getUserId()).compose(RxHelper.getObservaleTransformer())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {

                                if (mLoadingDialog == null) {
                                    mLoadingDialog = LoadingDialogUtils.createLoadingDialog(mContext, "");
                                }
                                LoadingDialogUtils.show(mLoadingDialog);

                            }
                        }).subscribeOn(AndroidSchedulers.mainThread());

                HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<ChildrenBeanX>(mContext) {

                    @Override
                    protected void _onNext(StatusCode<ChildrenBeanX> listStatusCode) {

                        LoadingDialogUtils.closeDialog(mLoadingDialog);
                        mYrecycleviewContentClassifyFragment.setReFreshComplete();

                        ChildrenBeanX childrenBeanX = listStatusCode.getData();
                        if (childrenBeanX!=null &&childrenBeanX.getChildren().size()>0){
                            mYrecycleviewContentClassifyFragment.setLoadMoreEnabled(true);
                            mYrecycleviewContentClassifyFragment.setReFreshEnabled(true);
                            mContentChildrenBeanList.clear();
                            mContentChildrenBeanList.add(childrenBeanX);
                            mClassifyContentRecyclerViewAdapter.setType(1);
                            currentPager=1;
                            mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                        }else {
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.makeText(message);
                        LoadingDialogUtils.closeDialog(mLoadingDialog);
                        mYrecycleviewContentClassifyFragment.setReFreshComplete();
                    }
                },"",lifecycleSubject,false,true);

            }

            @Override
            public void onLoadMore() {

                Observable observable = ApiUtils.getApi().getClassrecommentgoods(mCategoryId, Contans.defaultToken, currentPager+1, Contans.rows,BaseApplication.getUserId()).compose(RxHelper.getObservaleTransformer())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {

                                if (mLoadingDialog == null) {
                                    mLoadingDialog = LoadingDialogUtils.createLoadingDialog(mContext, "");
                                }
                                LoadingDialogUtils.show(mLoadingDialog);
                            }
                        }).subscribeOn(AndroidSchedulers.mainThread());

                HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<ChildrenBeanX>(mContext) {

                    @Override
                    protected void _onNext(StatusCode<ChildrenBeanX> listStatusCode) {


                        LoadingDialogUtils.closeDialog(mLoadingDialog);
                        mYrecycleviewContentClassifyFragment.setloadMoreComplete();

                        ChildrenBeanX childrenBeanX = listStatusCode.getData();
                        if (childrenBeanX!=null &&childrenBeanX.getChildren().size()>0){
                            mYrecycleviewContentClassifyFragment.setLoadMoreEnabled(true);
                            mYrecycleviewContentClassifyFragment.setReFreshEnabled(true);
                         //   mContentChildrenBeanList.add(childrenBeanX);
                            List<ChildrenBean> children = childrenBeanX.getChildren();
                            mContentChildrenBeanList.get(0).getChildren().addAll(children);
                            mClassifyContentRecyclerViewAdapter.setType(1);

                            mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                            currentPager+=1;
                        }else {
                            ToastUtils.makeText("没有更多数据了");
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.makeText(message);
                        LoadingDialogUtils.closeDialog(mLoadingDialog);
                        mYrecycleviewContentClassifyFragment.setloadMoreComplete();


                    }
                },"",lifecycleSubject,false,true);


            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        Bundle arguments = getArguments();
        ClassifyBean classifyBean = arguments.getParcelable(Contans.INTENT_DATA);
        mYrecycleviewMenuClassifyFragment.setLoadMoreEnabled(false);
        mYrecycleviewMenuClassifyFragment.setReFreshEnabled(false);

        mClassifyMenuRecyclerViewAdapter = new ClassifyMenuRecyclerViewAdapter(mContext,classifyBean.getChildren());

        //设置布局管理器
        mYrecycleviewMenuClassifyFragment.setLayoutManager(new LinearLayoutManager(mContext));
        //设置adapter
        mYrecycleviewMenuClassifyFragment.setAdapter(mClassifyMenuRecyclerViewAdapter);
        //设置Item增加、移除动画
        mYrecycleviewMenuClassifyFragment.setItemAnimator(new DefaultItemAnimator());
        mClassifyMenuRecyclerViewAdapter.setOnItemClickListener(new ClassifyMenuRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(View v, int position, String categoryId) {
                mClassifyMenuRecyclerViewAdapter.setCheckPostion(position);
                mClassifyMenuRecyclerViewAdapter.notifyDataSetChanged();

                LogUtils.i(TAG,"点击的位置：："+position);
                if (position==0){
                    //推荐商品
                    currentPager=1;
                    Observable observable = ApiUtils.getApi().getClassrecommentgoods(categoryId, Contans.defaultToken, 1, Contans.rows, BaseApplication.getUserId()).compose(RxHelper.getObservaleTransformer())
                            .doOnSubscribe(new Consumer<Disposable>() {
                                @Override
                                public void accept(Disposable disposable) throws Exception {

                                    if (mLoadingDialog == null) {
                                        mLoadingDialog = LoadingDialogUtils.createLoadingDialog(mContext, "");
                                    }
                                    LoadingDialogUtils.show(mLoadingDialog);
                                }
                            }).subscribeOn(AndroidSchedulers.mainThread());

                          HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<ChildrenBeanX>(mContext) {

                              @Override
                              protected void _onNext(StatusCode<ChildrenBeanX> listStatusCode) {


                                  LoadingDialogUtils.closeDialog(mLoadingDialog);

                                  ChildrenBeanX childrenBeanX = listStatusCode.getData();
                                  if (childrenBeanX!=null &&childrenBeanX.getChildren().size()>0){
                                      mYrecycleviewContentClassifyFragment.setLoadMoreEnabled(true);
                                      mYrecycleviewContentClassifyFragment.setReFreshEnabled(true);
                                      mContentChildrenBeanList.clear();
                                      mContentChildrenBeanList.add(childrenBeanX);
                                      mClassifyContentRecyclerViewAdapter.setType(1);
                                      mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                                  }else {
                                      mContentChildrenBeanList.clear();
                                      mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                                  }
                              }

                              @Override
                        protected void _onError(String message) {
                               ToastUtils.makeText(message);
                               LoadingDialogUtils.closeDialog(mLoadingDialog);
                                  mContentChildrenBeanList.clear();
                                  mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();


                        }
                    },"",lifecycleSubject,false,true);


                }else {
//点击品牌
                    mYrecycleviewContentClassifyFragment.setLoadMoreEnabled(false);
                    mYrecycleviewContentClassifyFragment.setReFreshEnabled(false);
                    Observable observable = ApiUtils.getApi().getCategoryDetails(categoryId).compose(RxHelper.getObservaleTransformer())
                            .doOnSubscribe(new Consumer<Disposable>() {
                                @Override
                                public void accept(Disposable disposable) throws Exception {

                                    if (mLoadingDialog == null) {
                                        mLoadingDialog = LoadingDialogUtils.createLoadingDialog(mContext, "");
                                    }
                                    LoadingDialogUtils.show(mLoadingDialog);
                                }
                            }).subscribeOn(AndroidSchedulers.mainThread());

                    HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<List<ChildrenBeanX>>(mContext) {
                        @Override
                        protected void _onNext(StatusCode<List<ChildrenBeanX>> listStatusCode) {
                            LoadingDialogUtils.closeDialog(mLoadingDialog);
                            List<ChildrenBeanX> children = listStatusCode.getData();
                            if (children!=null &&children.size()>0){
                                mContentChildrenBeanList.clear();
                                mContentChildrenBeanList.addAll(children);

                                mClassifyContentRecyclerViewAdapter.setType(2);
                                mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                            }else {
                                mContentChildrenBeanList.clear();
                                mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                            }

                        }
                        @Override
                        protected void _onError(String message) {
                            LoadingDialogUtils.closeDialog(mLoadingDialog);
                            ToastUtils.makeText(message);
                            mContentChildrenBeanList.clear();
                            mClassifyContentRecyclerViewAdapter.notifyDataSetChanged();
                        }
                    },"",lifecycleSubject,false,true);


                }
            }
        });
        mContentChildrenBeanList.clear();
        mContentChildrenBeanList.addAll(classifyBean.getChildren().get(0).getChildren());
        mClassifyContentRecyclerViewAdapter = new ClassifyContentRecyclerViewAdapter(mContext,mContentChildrenBeanList);
        mClassifyContentRecyclerViewAdapter.setType(1);
        mCategoryId=classifyBean.getChildren().get(0).getId()+"";
        //设置布局管理器
        mYrecycleviewContentClassifyFragment.setLayoutManager(new LinearLayoutManager(mContext));
        //设置adapter
        mYrecycleviewContentClassifyFragment.setAdapter(mClassifyContentRecyclerViewAdapter);
        //设置Item增加、移除动画
        mYrecycleviewContentClassifyFragment.setItemAnimator(new DefaultItemAnimator());
        mYrecycleviewContentClassifyFragment.setLoadMoreEnabled(true);
        mYrecycleviewContentClassifyFragment.setReFreshEnabled(true);

    }


}
